package com.hxt.z.web.servlet;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;

import com.alibaba.fastjson.JSONObject;
import com.hxt.framework.msg.ResponseMsg;
import com.hxt.framework.utils.DateUtil;
import com.hxt.framework.utils.JsonUtil;
import com.hxt.util.CONSTANT;
import com.hxt.util.LogsUtil;
import com.hxt.util.MinioClientUtils;
import com.hxt.util.StringUtil;
import com.hxt.xt.service.base.msg.Result;

/**
 * 上传excel到当前项目目录下的upload文件夹下
 */
@WebServlet("/upload")
public class UploadServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html");
		resp.setCharacterEncoding("UTF-8");
		doPost(req, resp);
	}

	@SuppressWarnings("rawtypes")
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		InputStream in = null;
		String uploadURL = "";
		String uploadKEY = "";
		String fileName = "";
		String ext = "";// 扩展名
		DiskFileItemFactory fac = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(fac);
		upload.setHeaderEncoding("utf-8");
		List<FileItem> fileList = null;

		try {
			fileList = upload.parseRequest(request);

			Iterator it = fileList.iterator(); // 遍历文件对象
			while (it.hasNext()) {
				FileItem item = (FileItem) it.next();
				String orifileName = item.getName();// 1.png
				fileName = StringUtil.getUUID();

				in = item.getInputStream();
				//
				String baseUploadPath = CONSTANT.COMMON.UPLOAD_FILE_PATH;
				if (orifileName.lastIndexOf(".") >= 0) {
					ext = orifileName.substring(orifileName.lastIndexOf("."));
				}
				
				// 获取的文件输入流上传minio

				Map<String, String> result = MinioClientUtils.upload(in, ext, item.getContentType());
				uploadURL = result.get("url").toString();
				uploadKEY = result.get("key").toString();
				LogsUtil.printString(this.getClass(), "uploadURL", uploadURL);
				LogsUtil.printString(this.getClass(), "uploadKEY", uploadKEY);
				
				//当上传完了Minio后本地保存一份，在做产品导入的时候用
				FileUtils.copyInputStreamToFile(in, new File(baseUploadPath, uploadKEY));// 保存上传的文件到指定路径下

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

		}
		JSONObject obj = new JSONObject();
		JSONObject result = new JSONObject();

		obj.put("retCode", "200");
		obj.put("retMsg", "处理成功");
		obj.put("errMsg", null);
		obj.put("reqSeq", DateUtil.getToday());

		result.put("url", uploadURL);
		result.put("key", uploadKEY);
		obj.put("result", result);
			
		System.out.println(obj.toJSONString());
		out.print(obj.toJSONString());
		
		
		
		out.flush();
		out.close();
		in.close();

	}

	public static void main(String[] args) {
	}

}